<?php

class Blog_ReplyController extends Ext_Controller_Action_Front
{
    public function init()
    {
        parent::init();

    }

    public function listByJsonAction()
    {
        $viewResult = new Ext_View_Result();
        
        $articleId = $this->filter->filter($this->_request->get('articleId'));
        
        $count = App_Model_Reply::getInstance()->getCount($articleId);
        $list  = App_Model_Reply::getInstance()->getList($articleId);
        $adapter = new Zend_Paginator_Adapter_DbSelect($list);
        $adapter->setRowCount($count);
        $paginator = new Zend_Paginator($adapter);
        $paginator
              //->setCurrentPageNumber(1)
              ->setItemCountPerPage(12)
              ->setCurrentPageNumber($this->_getParam('page'))
            //->setDefaultPageRange(20)
            ;
        $replies = array();
        foreach ($paginator as $key=>$item)
        {
            $_articleId = $item['articleId'];
            
            $_article = new Ext_Article_Show($_articleId);
            $_article->buildDescription()->buildAttachemnts()->buildTags();

            $replies[] = $_article;
        }
        
        $this->view->replies = $replies;
        
        $viewResult->setResult(1);
    }

    public function listAction()
    {
        $articleId = $this->filter->filter($this->_request->get('articleId'));

        $article = new Ext_Article_Show($articleId);
        $article->isExcerpt()->buildDescription()->buildAttachemnts()->buildTags()->buildFamily();

        $author = Ext_User::getInstance()->show($article->author->userId)->buildAvatar()->buildUrl();
        $buddyCheckor = new Ext_Buddy_Check($article->author->userId);
        $isMyBuddy = $buddyCheckor->isMyBuddy();
        
        $count = App_Model_Reply::getInstance()->getCount($articleId);
        $list  = App_Model_Reply::getInstance()->getList($articleId);
        $adapter = new Zend_Paginator_Adapter_DbSelect($list);
        $adapter->setRowCount($count);
        $paginator = new Zend_Paginator($adapter);
        $paginator
              //->setCurrentPageNumber(1)
              ->setItemCountPerPage(12)
              ->setCurrentPageNumber($this->_getParam('page'))
            //->setDefaultPageRange(20)
            ;
        $replies = array();
        foreach ($paginator as $key=>$item)
        {
            $_articleId = $item['articleId'];
            
            $_article = new Ext_Article_Show($_articleId);
            $_article->buildDescription()->buildAttachemnts()->buildTags();

            $replies[] = $_article;
        }

        
        $this->view->author    = $author;
        $this->view->isMyBuddy = $isMyBuddy;
        $this->view->replies   = $replies;
        $this->view->article   = $article;
        $this->view->paginator = $paginator;
    }
}

